package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.ProductOrderQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.ProductOrderDao;
import cn.org.eshow.model.ProductOrder;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("productOrderDao")
public class ProductOrderDaoHibernate extends GenericDaoHibernate<ProductOrder, Integer>
        implements ProductOrderDao {


    public ProductOrderDaoHibernate() {
        super(ProductOrder.class);
    }

    @Override
    public List<ProductOrder> list(ProductOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<ProductOrder> search(ProductOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(ProductOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(ProductOrder.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, ProductOrderQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startUpdateTime != null) {
                rule.add(Restrictions.ge("updateTime", query.startUpdateTime));
            }
            if (query.endUpdateTime != null) {
                rule.add(Restrictions.le("updateTime", query.endUpdateTime));
            }
            if (StringUtils.isNotEmpty(query.category)) {
                rule.add(Restrictions.like("category", query.category, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.no)) {
                rule.add(Restrictions.like("no", query.no, MatchMode.ANYWHERE));
            }
            if (query.minPrice != null) {
                rule.add(Restrictions.ge("price", query.minPrice));
            }
            if (query.maxPrice != null) {
                rule.add(Restrictions.le("price", query.maxPrice));
            }
            if (query.payment != null) {
                rule.add(Restrictions.eq("payment", query.payment));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (StringUtils.isNotEmpty(query.states)) {
                String[] vals = StringUtils.split(query.states, ",");
                Integer[] bytes = new Integer[vals.length];
                for (int i = 0; i < vals.length; i++) {
                    bytes[i] = Integer.valueOf(vals[i].trim());
                }
                rule.add(Restrictions.in("state", bytes));
            }
            if (StringUtils.isNotEmpty(query.statements)) {
                rule.add(Restrictions.like("statements", query.statements, MatchMode.ANYWHERE));
            }
            if (query.payState != null) {
                rule.add(Restrictions.eq("payState", query.payState));
            }
            if (query.commentState != null) {
                rule.add(Restrictions.eq("commentState", query.commentState));
            }
            if (query.refundState != null) {
                rule.add(Restrictions.eq("refundState", query.refundState));
            }
            if (query.deliveryState != null) {
                rule.add(Restrictions.eq("deliveryState", query.deliveryState));
            }
            if (StringUtils.isNotEmpty(query.callback)) {
                rule.add(Restrictions.like("callback", query.callback, MatchMode.ANYWHERE));
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (query.province != null) {
                rule.add(Restrictions.like("province", query.province, MatchMode.ANYWHERE));
            }
            if (query.city != null) {
                rule.add(Restrictions.like("city", query.city, MatchMode.ANYWHERE));
            }
            if (query.district != null) {
                rule.add(Restrictions.like("district", query.district, MatchMode.ANYWHERE));
            }
            if (query.userCouponId != null) {
                rule.add(Restrictions.eq("userCoupon.id", query.userCouponId));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
